<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jsoop继承</title>
</head>
<body>
    
</body>
<script>
    /*java
    class Cat extends Animal{

    }
    */
   //把b对象的所有值复制给a对象     b--->a
   //a.name=a[name]
   Object.copy=function(a,b){
        for(var attr in b){
            a[attr]=b[attr];
        }
        return a;
   }
   //继承方法
   Object.prototype.extends=function(o){
        return Object.copy(this,o);    //当前对象继承o
   }
   var temo={
       name:"temo",
       age:18,
       fun:function(){
           console.log("方法");
       }
   };
   var dema={};
//    Object.copy(dema,temo);//复制
   dema.extends(temo);//继承
   console.log(temo);
   console.log(dema);
   dema.fun()
   //定义一个类 cat={} dog复制给cat
   var Animal=function(name,type){
        this.name=name;
        this.type=type;
   }
   /*
    Animal.prototype.color="彩色";
    Animal.prototype.fight=function(){
        console.log("动物打架了"+this.type);
    }
    */
   Animal.prototype={
       color:"彩色",
       fight:function(){
           console.log("动物打架:"+this.name);
       }
   }
   var dog=new Animal("小狗","哈士奇");
   //复制
   var cat={};
   Object.copy(cat,dog);
   console.log(cat);
   dog.fight()

   //形状类(继承示例2)
    var Shape=function(){
        this.draw=function(){
            //调用下方空方法
            this._draw();
        }
        //空方法
        this._draw=function(){}
   }
   //矩形
   var Rect=function(){};
//    var xz=new Shape();//父类对象
   Rect.prototype=(new Shape()).extends({
       width:100,
       height:200,
       _draw:function(){
           console.log("画个矩形");
       }
   });
   var rec=new Rect();
   rec._draw();
   //圆形
   var Yuan=function(){};
//    var xz=new Shape();//父类对象
   Yuan.prototype=(new Shape()).extends({
       width:100,
       height:100,
       _draw:function(){
           console.log("画个圆形");
       }
   });
   var yuan=new Yuan();
   yuan._draw();
</script>
</html>